package com.surelution.projects.crm
import org.codehaus.groovy.grails.commons.ConfigurationHolder

class MarketPictureController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	def pictureService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [marketPictureInstanceList: MarketPicture.list(params), marketPictureInstanceTotal: MarketPicture.count()]
    }

    def create = {
        def marketPictureInstance = new MarketPicture()
        marketPictureInstance.properties = params
        return [marketPictureInstance: marketPictureInstance]
    }

    def save = {
			def marketPictureInstance
			def title = params.title
			def description = params.description
			def file = params.pictureFile
			def marketInstance = Market.get(params.market.id)
			flash.message = "${message(code: 'default.null.picture')}"
			if(title == "" && !file.isEmpty()) {
				flash.message = "${message(code: 'default.not.found.title')}"
				render(view: "create", model: [marketPictureInstance: marketPictureInstance])
			}
			if(title != "" && !file.isEmpty()) {
				Picture picture = pictureService.storePicture(file.inputStream)
				picture.title = title
				picture.description = description
				picture.uploadTime = new Date()
				picture.save(flush:true)
				marketPictureInstance = new MarketPicture()
				marketPictureInstance.market = marketInstance
				marketPictureInstance.picture = picture
				try{
					marketInstance.addToPictures(marketPictureInstance)
					marketPictureInstance.save(flush:true)
				}catch(e) {
					e.printStackTrace()
				}
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), params.id])}"
            redirect(action: "show", id: marketInstance.id, controller:"market")
        }
        else {
            redirect(action: "show", id: marketInstance.id, controller:"market")
        }
    }

    def show = {
        def marketPictureInstance = MarketPicture.get(params.id)
        if (!marketPictureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), params.id])}"
            redirect(action: "list")
        }
        else {
            [marketPictureInstance: marketPictureInstance]
        }
    }

    def edit = {
        def marketPictureInstance = MarketPicture.get(params.id)
        if (!marketPictureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [marketPictureInstance: marketPictureInstance]
        }
    }

    def update = {
        def marketPictureInstance = MarketPicture.get(params.id)
        if (marketPictureInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketPictureInstance.version > version) {
                    marketPictureInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'marketPicture.label', default: 'MarketPicture')] as Object[], "Another user has updated this MarketPicture while you were editing")
                    render(view: "edit", model: [marketPictureInstance: marketPictureInstance])
                    return
                }
            }
            marketPictureInstance.properties = params
			def market = Market.get(params.market.id)
            if (!marketPictureInstance.hasErrors() && marketPictureInstance.save(flush: true)) {
					marketPictureInstance.market = market
					marketPictureInstance.picture.title = params.title
					marketPictureInstance.picture.description = params.description
					
					try{
						marketPictureInstance.save(flush:true)
					}catch(e) {
						e.printStackTrace()
					}
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), marketPictureInstance.id])}"
                redirect(action: "show", id: market.id,controller:"market")
            }
            else {
                render(view: "edit", model: [marketPictureInstance: marketPictureInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def marketPictureInstance = MarketPicture.get(params.id)
        if (marketPictureInstance) {
            try {
                marketPictureInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketPicture.label', default: 'MarketPicture'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def name = {
		def marketPicture = MarketPicture.get(params.id)
		def picture = marketPicture.picture
		def filePath = pictureService.loadPicture(picture)
		
		FileInputStream fos = new FileInputStream(filePath)
		response.contentType = "image/${picture.type}"
		response.outputStream << fos
		response.outputStream.flush()
		fos.close()
		return
	}
}
